//
//  QLFollowManager.m
//  WeiMiLan
//
//  Created by anan on 14/11/25.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "QLFollowManager.h"

@implementation QLFollowManager

-(id) init
{
    self = [super init];
    if (self) {
        self.follows = [[NSMutableArray alloc] init];
        self.followDic =[[NSMutableDictionary alloc] init];
    }
    return self;
}

static QLFollowManager *instance;

+ (QLFollowManager*)shareInstance
{
    if (instance == nil) {
        instance = [[QLFollowManager alloc] init];
    }
    return instance;
}

- (void)addFollowWithUserId:(int)userId myUserId:(int)myUserId callback:(QLHttpResultBlock)callback
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic ql_setInterge:myUserId forKey:@"userId"];
    [dic ql_setInterge:userId forKey:@"followId"];
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [requestManager GET:@"http://ping.vsmu.cn/app/Interface/User?type=9002" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self loadAllWithUserId:myUserId callback:^(BOOL isCache, id data) {
            [SVProgressHUD showSuccessWithStatus:@"关注添加成功"];
            if (callback) {
                callback(NO,self.follows);
            }
        }];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showSuccessWithStatus:@"关注添加失败"];
        if (callback) {
            callback(NO,nil);
        }
    }];
}

- (BOOL)isFollowWithUserId:(int)userId myUserId:(int)myUserId
{
    NSString *key = [NSString stringWithFormat:@"%d_%d",myUserId,userId];
    return [self.followDic ql_hasObjectForKey:key];
}

- (void)deleteFollowWithUserId:(int)userId myUserId:(int)myUserId callback:(QLHttpResultBlock)callback
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic ql_setInterge:myUserId forKey:@"userId"];
    [dic ql_setInterge:userId forKey:@"followId"];
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [requestManager GET:@"http://ping.vsmu.cn/app/Interface/User?type=9003" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [SVProgressHUD showSuccessWithStatus:@"关注删除成功"];
        NSString *key = [NSString stringWithFormat:@"%d_%d",myUserId,userId];
        [self.followDic removeObjectForKey:key];
        if (callback) {
            callback(NO,self.follows);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showSuccessWithStatus:@"关注删除失败"];
        if (callback) {
            callback(NO,nil);
        }
    }];
}

- (void)loadAllWithUserId:(int)userId callback:(QLHttpResultBlock)callback
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic ql_setInterge:userId forKey:@"userId"];
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [requestManager GET:@"http://ping.vsmu.cn/app/Interface/User?type=9006" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self setFollows:responseObject[@"USER_INFO_RSP"][@"USER_INFOS"] myUserId:userId];
        if (callback) {
            callback(NO,self.follows);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (callback) {
            callback(NO,nil);
        }
    }];
}

- (void)setFollows:(NSMutableArray *)follows myUserId:(int)myUserId
{
    _follows = follows;
    [self.followDic removeAllObjects];
    if (follows) {
        for (NSDictionary *dic in follows) {
            NSString *key = [NSString stringWithFormat:@"%d_%d",myUserId,[dic ql_intForKey:@"ID"]];
            [self.followDic setObject:dic forKey:key];
        }
    }
}

@end
